﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using Perspective.Wpf3D.Shapes;
using System.Windows.Media.Animation;
using System.Threading;
using WPFMediaKit.DirectShow.Controls;

using Perspective.Core.Wpf;
using Petzold.Media3D;

namespace Wpf3dCollaborative
{
    /// <summary>
    /// Logique d'interaction pour Webcam.xaml
    /// </summary>
    public partial class Cam : Square3D
    {

        void MyMediaOpened(object sender, EventArgs e)
        {
            Console.WriteLine("MEdia Opened ID:" + Id);
            Play(); 
          

        }
        void MyMediaFailled(object sender, EventArgs e)
        {
            Console.WriteLine("MEdia Failed ID:" + Id);
            Player = null;
            // TODO : attention Thread Appelant STA
            // On vérifie que le thread à un droit d'accès sur WPF
            this.Dispatcher.Invoke(
              System.Windows.Threading.DispatcherPriority.Normal,
              new Action(
                delegate()
                {

                    Player = new Image();
                    string uriSource = "pack://application:,,,/wpf3dCollaborative;component/Images/failed.png";
                    ((Image)Player).Source = new ImageSourceConverter().ConvertFromString(uriSource) as ImageSource;
                    Content.Visual = (Image)Player;                    
                }));



        }


       


        private void Square3D_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Transforms.StartDrag(e, IsFullScreen, Helper3D.GetViewport3D(this));
            CaptureMouse();
            Focus();
           
        }


        private void Square3D_MouseMove(object sender, MouseEventArgs e)
        {

            Transforms.DragMove(e, IsFullScreen, Helper3D.GetViewport3D(this));

        }



        private void Square3D_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            Transforms.MouseWheel(e, IsFullScreen, Helper3D.GetViewport3D(this));
           
        }



        public void Square3D_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Transforms.IsTracking = false;
            AfficherOptionCam(e.GetPosition(App.windows), App.windows.Menu);
            ReleaseMouseCapture();
        }

        private void Square3D_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            Transforms.FullScreen(IsFullScreen, IsWideCam);
        }

        private void Square3D_LostMouseCapture(object sender, MouseEventArgs e)
        {
            // Si le déplacement est annulé on restaure les valeur
            Transforms.LostMouseCapture();
        }


        private void Square3D_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Transforms.IsTracking)
                return;

            // Fin du déplacement de la souris à l'appuis de la touche echap
            if (e.Text.IndexOf('\x1B') != -1)
                ReleaseMouseCapture();
        }




    }
}
